﻿using CIS.Common.Dto.Dtos.Equipments.Commands;
using CIS.Common.Enums;
using CIS.Common.Grpc;
using CIS.Server.Common.Entitys;
using CIS.Server.Common.Enums;
using CIS.Server.Common.Responses;
using CIS.Server.Manager.Equipment;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net;

namespace CIS.Server.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class PortController : ControllerBase
    {
        private readonly IEquipmentManager _equipmentManager;

        public PortController(IEquipmentManager equipmentManager)
        {
            _equipmentManager = equipmentManager;
        }

        [HttpGet]
        public async Task<ResponseResult> GetPorts()
        {
            ResponseResult response = new ResponseResult();
            try
            {
                List<PortEntity> ports = await _equipmentManager.GetAllPortsAsync();
                if(ports !=null &&  ports.Count > 0)
                {
                    ports = ports.OrderBy(p => p.PortID).ToList();
                    response.IsSuccess = true;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Success";
                    response.Data = ports;
                }
                else
                {
                    response.IsSuccess = false;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Failed Because Can't Find Any Port";
                }
                return response;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Message = "Operation Is Failed Because Server Is Error";
                return response;
            }
        }

        [HttpPost]
        public async Task<ResponseResult> SendPortControlCommand([FromBody] string parameter)
        {
            ResponseResult response = new ResponseResult();
            var para = JsonConvert.DeserializeObject<PortControlCommandDto>(parameter);
            string controlType = Enum.GetName(typeof(PortControlType),para.PortControlCommand);
            switch (controlType)
            {
                case "UnClamp":
                    response.IsSuccess = true;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Port UnClamp Operation Is Success";
                    response.Data=await _equipmentManager.SendPortCommand(para.SourceId, para);
                    break;
                case "Clamp":
                    response.IsSuccess = true;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Data =await _equipmentManager.SendPortCommand(para.SourceId, para);
                    response.Message = "Port Clamp Operation Is Success";
                    break;
            }
            return response;
        }
    }
}
